new Vue({
  el: '#main',
  data: {
    teamName: '',
    teamRank: '-',
    myRank: '-',
    attendPerson: '-',
    popularity: '-',
    avgProfitRate: '',
    updateTime: '',
    blankRate: '-',
    gameStep: ''
  },
  mounted: function () {
    this.fetchData()
    this.getShareData()
  },
  methods: {
    fetchData: function () {
      var apiAddress = this.getUrlParam('url')
      var _this = this
      $.ajax({
        methods: 'GET',
        url: apiAddress ,
        dataType: 'JSONP',
        jsonpCallback: 'niurenshare',
        success (res) {
          console.log(res);
          const { teamDetail, myRankInfo } = res
          const dateArr = JSON.parse(teamDetail.avgProfitDays || "[]")
          _this.teamName = teamDetail.GAME_NAME
          _this.attendPerson = teamDetail.GAME_ATTEND_PERSON
          _this.teamRank = teamDetail.avgProfitRank || '-'
          _this.updateTime = parseTime(dateArr[dateArr.length - 1])
          _this.myRank = myRankInfo.ranknum > 0 ? myRankInfo.ranknum : '-'
          _this.gameStep = teamDetail.GAME_STEP
          _this.popularity = teamDetail.popularity || '-'
          _this.blankRate = parseFloat(teamDetail.shortRate || 0).toFixed(2)
        }
      })
    },

    getShareData: function () {
      var title = this.getUrlParam('title')
      var content = this.getUrlParam('content')
      var icon = 'https://dl.app.gtja.com/nrng/shareimages/niuniu.jpg'
      window.weixinShare(title, content, icon, window.location.href)
    },

    getUrlParam: function (name) {
      let url = decodeURI(location.search)
      let theRequest = new Object()
      if (Number(url.indexOf('?')) !== -1) {
          let str = url.substr(1)
          let strs = str.split('&')
          for (let i = 0; i < strs.length; i++) {
              theRequest[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1])
          }
      }
      return theRequest[name]
    }
  },
  computed: {
    inprogress: function () {
      return this.gameStep === '4' && this.teamRank === '1'
    },

    footerHeight: function () {
      return {
        height: window.innerWidth / 748 * 222
      }
    }
  }

})

function getUrlParam (key) {
  let url = decodeURI(location.search)
  let theRequest = new Object()
  if (Number(url.indexOf('?')) !== -1) {
      let str = url.substr(1)
      let strs = str.split('&')
      for (let i = 0; i < strs.length; i++) {
          theRequest[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1])
      }
  }
  return theRequest[key]
}

function parseTime (time) {
  return !!time ? time.slice(0,4) + '-' + time.slice(4, 6) + '-' + time.slice(6, 8) : '-'
}

function download () {
  var userCode = getUrlParam('userCode')
  var activityCode = getUrlParam('activitycode')
  var shareId = getUrlParam('shareId')

  if (activityCode !== "" && typeof(activityCode) !== 'undefined') {
    window.location.href = 'https://fy.gtja.com/m/app/nrng/register.jsp?userCode=' + userCode + '&activityCode=' + activityCode + '&shareId=' + shareId
  } else if (userCode !== '' && typeof(userCode) !== 'undefined') {
    window.location.href = 'https://dl.app.gtja.com/nrng/share/share.register.html?userCode=' + userCode
  } else {
    window.location.href = 'https://yyz.gtja.com/dl?cid=yyz'
  }
}
